package com.situation.exception;

import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.situation.util.RespUtil;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class AllExceptionHandler {

    /**
     * token错误，请重新登入
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = AccountException.class)
    public RespUtil accountException(Exception e) {
        System.out.println("发生了一个异常" + e);
        return RespUtil.account();
    }

    /**
     * 账号不存在
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = AuthenticationException.class)
    public RespUtil authenticationException(Exception e) {
        System.out.println("发生了一个异常" + e);
        return RespUtil.unknownAccount();
    }

    /**
     * 账号已被锁定
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = LockedAccountException.class)
    public RespUtil lockedAccountException(Exception e) {
        System.out.println("发生了一个异常" + e);
        return RespUtil.lockedAccount();
    }

    /**
     * 密码错误 504
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = IncorrectCredentialsException.class)
    public RespUtil incorrectCredentialsException(Exception e) {
        System.out.println("发生了一个异常" + e);
        return RespUtil.incorrectCredentials();
    }

    @ExceptionHandler(value = IllegalAccessException.class)
    public RespUtil illegalAccessException(Exception e) {
        System.out.println("发生了一个异常" + e);
        return RespUtil.fail();
    }

    @ExceptionHandler(value = AuthorizationException.class)
    public RespUtil authorizationException(Exception e) {
        System.out.println("发生了一个异常" + e);
        return RespUtil.unauthorized();
    }

    @ExceptionHandler(value = JWTVerificationException.class)
    public RespUtil jwtVer(Exception e) {
        System.out.println("发生了一个异常" + e);
        return RespUtil.account();
    }

    @ExceptionHandler(value = JWTDecodeException.class)
    public RespUtil jwtDec(Exception e) {
        System.out.println("发生了一个异常" + e);
        return RespUtil.account();
    }

    @ExceptionHandler(value = Exception.class)
    public RespUtil exceptionHandler(Exception e) {
        System.out.println("发生了一个异常" + e);
        return RespUtil.notFound();
    }

}
